package cn.iocoder.yudao.module.station.controller.admin.cylinderblacklist;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.station.controller.admin.cylinderblacklist.vo.*;
import cn.iocoder.yudao.module.station.dal.dataobject.cylinderblacklist.CylinderBlacklistDO;
import cn.iocoder.yudao.module.station.service.cylinderblacklist.CylinderBlacklistService;

@Tag(name = "管理后台 - 钢瓶黑名单")
@RestController
@RequestMapping("/station/cylinder-blacklist")
@Validated
public class CylinderBlacklistController {

    @Resource
    private CylinderBlacklistService cylinderBlacklistService;

    @PostMapping("/create")
    @Operation(summary = "创建钢瓶黑名单")
    @PreAuthorize("@ss.hasPermission('station:cylinder-blacklist:create')")
    public CommonResult<Integer> createCylinderBlacklist(@Valid @RequestBody CylinderBlacklistSaveReqVO createReqVO) {
        return success(cylinderBlacklistService.createCylinderBlacklist(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钢瓶黑名单")
    @PreAuthorize("@ss.hasPermission('station:cylinder-blacklist:update')")
    public CommonResult<Boolean> updateCylinderBlacklist(@Valid @RequestBody CylinderBlacklistSaveReqVO updateReqVO) {
        cylinderBlacklistService.updateCylinderBlacklist(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钢瓶黑名单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('station:cylinder-blacklist:delete')")
    public CommonResult<Boolean> deleteCylinderBlacklist(@RequestParam("id") Integer id) {
        cylinderBlacklistService.deleteCylinderBlacklist(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钢瓶黑名单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('station:cylinder-blacklist:query')")
    public CommonResult<CylinderBlacklistRespVO> getCylinderBlacklist(@RequestParam("id") Integer id) {
        CylinderBlacklistDO cylinderBlacklist = cylinderBlacklistService.getCylinderBlacklist(id);
        return success(BeanUtils.toBean(cylinderBlacklist, CylinderBlacklistRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钢瓶黑名单分页")
    @PreAuthorize("@ss.hasPermission('station:cylinder-blacklist:query')")
    public CommonResult<PageResult<CylinderBlacklistRespVO>> getCylinderBlacklistPage(@Valid CylinderBlacklistPageReqVO pageReqVO) {
        PageResult<CylinderBlacklistDO> pageResult = cylinderBlacklistService.getCylinderBlacklistPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CylinderBlacklistRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钢瓶黑名单 Excel")
    @PreAuthorize("@ss.hasPermission('station:cylinder-blacklist:export')")
    @OperateLog(type = EXPORT)
    public void exportCylinderBlacklistExcel(@Valid CylinderBlacklistPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CylinderBlacklistDO> list = cylinderBlacklistService.getCylinderBlacklistPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "钢瓶黑名单.xls", "数据", CylinderBlacklistRespVO.class,
                        BeanUtils.toBean(list, CylinderBlacklistRespVO.class));
    }

}